﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyNhaThuoc.Models;
using QuanLyNhaThuoc.HelperClass;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace QuanLyNhaThuoc.Controllers
{
    public class CategoryController : Controller
    {
        PMOEntities PMOEn = new PMOEntities();

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region Check session login
            if (HttpContext.Session["USERID"] == null)
            {
                Response.Redirect("~/Account/LogIn");
            }
            #endregion

            #region Check function permission
            string controller = RouteData.Values["controller"].ToString();
            string action = RouteData.Values["action"].ToString();

            var query = PMOEn.CheckFunctionPermission(Session["USERID"].ToString(), controller, action).ToList();
            if (query.Count != 0)
            {
                ViewBag.Title = query[0].Title;
            }
            else
            {
                Response.Redirect("~/Error/Denied");

            }
            #endregion

            base.OnActionExecuting(filterContext);
        }
        //
        // GET: /Category/


        private ProductModel BindData(int page, int pageSize)
        {
            System.Data.Objects.ObjectParameter output = new System.Data.Objects.ObjectParameter("total", typeof(int));

            ProductModel proMo = new ProductModel();

            if (Session["KEYSEARCH"] != null)
            {
                proMo.lstProduct = PMOEn.GetProduct("", Session["CORPID"].ToString(), page, pageSize, output, Session["KEYSEARCH"].ToString(), Session["VALUESEARCH"].ToString()).ToList();
                proMo.searchText = Session["VALUESEARCH"].ToString();
            }
            else
            {
                proMo.lstProduct = PMOEn.GetProduct("", Session["CORPID"].ToString(), page, pageSize, output, "ALL", "").ToList();
            }

            var result = PMOEn.LoadAllCodeByName(SYSVAR.PRODUCTSEARCH, "").ToList();

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in result)
            {
                SelectListItem sl = new SelectListItem();
                sl.Text = item.Content;
                sl.Value = item.Value;

                if (Session["KEYSEARCH"] != null)
                {
                    if (item.Value == Session["KEYSEARCH"].ToString())
                    {
                        sl.Selected = true;
                    }
                }

                list.Add(sl);
            }

            proMo.lstProductSearch = (IEnumerable<SelectListItem>)list;

            var model = new PagingModel()
            {
                CurrentPageIndex = page,
                PageSize = pageSize
            };

            // Determine the total number of products being paged through (needed to compute PageCount)
            model.TotalRecordCount = int.Parse(output.Value.ToString());

            proMo.paging = model;

            return proMo;
        }

        public ActionResult Product(int page = 1, int pageSize = 15)
        {
            Session["KEYSEARCH"] = null;
            Session["VALUESEARCH"] = null;

            ProductModel proMo = new ProductModel();
            proMo = BindData(page, pageSize);
            return View(proMo);

        }

        public ActionResult ProductAdd()
        {
            ProductModel proMo = new ProductModel();
            var ProductTypeResult = PMOEn.GetProductType("");

            /* load Product Type */
            List<SelectListItem> list = new List<SelectListItem>();
            foreach(var item in ProductTypeResult)
            {
                SelectListItem sl = new SelectListItem();
                sl.Text = item.ProductTypeName;
                sl.Value = item.ProductTypeID.ToString();
                
                list.Add(sl);
            }
            list.Insert(0, new SelectListItem() { Value = "", Text = ConfigurationManager.AppSettings["SelectOne"].ToString() });
            /* end */

            /* load unit */
            var List_CCYID = PMOEn.LoadAllCodeByName(SYSVAR.CCYID, String.Empty);

            List<SelectListItem> _listCurrency = new List<SelectListItem>();

            foreach (var item in List_CCYID)
            {
                _listCurrency.Add(new SelectListItem()
                {
                    Text = item.Content,
                    Value = item.Value.ToString()
                });
            }

            _listCurrency.Insert(0, new SelectListItem() { Value = "", Text = ConfigurationManager.AppSettings["SelectOne"].ToString() });
           /* end */

            proMo.lstProductType = (IEnumerable<SelectListItem>)list;
            proMo.lstCCYID = (IEnumerable<SelectListItem>)_listCurrency;

            return View(proMo);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(FormCollection frm, int page = 1, int pageSize = 15)
        {           
            Session["KEYSEARCH"] = frm[0];
            Session["VALUESEARCH"] = frm[1];

            ProductModel proMo = new ProductModel();
            proMo = BindData(page, pageSize);
            return View(proMo);
        }

        [HttpGet]
        public ActionResult Search(int page = 1, int pageSize = 15)
        {
            ProductModel proMo = new ProductModel();
            proMo = BindData(page, pageSize);
            return View(proMo);
        }
    }
}
